<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>创建或修改文章</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../layui/css/layui.css" media="all">
</head>
<body>
  <form id="post_form" class="layui-form" action="" lay-filter="post_form" style="padding:20px;">
    <div class="layui-form-item">
      <label class="layui-form-label">文章标题</label>
      <div class="layui-input-inline">
        <input type="text" name="title" lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">文章内容</label>
      <div class="layui-input-inline" style="width:600px;">
          <textarea id="content" style="display: none;"></textarea>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">分类</label>
      <div class="layui-input-inline">
        <select name="category" lay-verify="required" id="post_category">
          <option value="">请选择分类</option>



        </select>
      </div>
    </div>

    <div class="layui-form-item">
      <div class="layui-input-block">
        <button class="layui-btn" lay-submit="" lay-filter="btn-submit">立即提交</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
      </div>
    </div>
  </form>

  <script src="../layui/layui.js"></script>  
  <script>
  layui.use(['form', 'layedit'], function(){
    var $ = layui.$;
    var form = layui.form;

    var layedit = layui.layedit;
    var editorIndex = layedit.build('content'); //建立编辑器
    //console.log(editorIndex);
    //var content=layedit.getText(editorIndex);
    //console.log(content);
    
    //监听提交
	form.on('submit(btn-submit)', function(data){
//	  console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
//	  console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
	  console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
//	  console.log(data.field.file)
	  var title=data.field["title"];
	 // console.log(title);
	  var content=layedit.getText(editorIndex);
	 // console.log(content);
	  //这样不能获取category
	  var category=data.field["category"];
	 // console.log(category);
	  //ajax请求
	  $.get("../post/addPost",{"title":title,"content":content,"category":category})
	 
	  return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
	});

    //自定义验证规则
    form.verify({
      title: function(value) {
        if(value.length < 5) {
          return '标题至少5个字符';
        }
      },
      content: function(value) {
        layedit.sync(editorIndex);
      }
    });
    
    //表单初始赋值
    form.val('post_form', {
      "category": "1"
    })
    
    
    // 页面加载完成之后，查询所有的分类信息，显示到下拉框
    $.get("../category/findAll", function(response) {
  	  var categories = response.data;
  	  
  	  // <option value='id'>name</option>
  	  for (var i = 0; i < categories.length; i++) {
  		  var item = categories[i];
  		  var option = "<option value='" + item.id + "'>" + item.name + "</option>";
  		  //将option动态的挂到select上
  		  $('#post_category').append(option);
  	  }
  	  //必须执行下面的渲染，不然option虽然能挂到seslect下，但是下拉框没有东西**
  	  form.render('select');
    });
    
    
  })
  </script>
</body>
</html>